﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms; 
using WoChu.RfClient.Entity;
using WoChu.RfClient.Duty;
using WoChu.RfClient.Common;
using WoChu.CF.BaseFrame.Util;

namespace WoChu.RfClient.Duty
{
    public partial class FrmLocationGoodsRelation : Form
    {
        public FrmLocationGoodsRelation()
        {
            InitializeComponent();
        }

        //private static List<LocationGoodsInfo> _lstLocationGoods = null;
        //public static List<LocationGoodsInfo> lstLocationGoods
        //{
        //    get
        //    {
        //        if (_lstLocationGoods == null) _lstLocationGoods = new List<LocationGoodsInfo>();
        //        return _lstLocationGoods;
        //    }
        //    set { _lstLocationGoods = value; }
        //}
  

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                 
                    if (WoChu.CF.BaseFrame.Util.MessageBoxEx.Question("是否提交？") == DialogResult.No)
                        return;
                   if (string.IsNullOrEmpty(txtLocationCode.Text))
                   {
                       MessageBox.Show("请先扫描货位！");
                       txtLocationCode.SelectAll();
                       return;
                   }
                   if (string.IsNullOrEmpty(txtGoodsCode.Text))
                   {
                       MessageBox.Show("请扫描商品！");
                       txtGoodsCode.SelectAll();
                       return;
                   }

                    var re = Duty.LocationGoodsUIRule.NewInstance.SetLocationGoods(LoginBean.Instance.USERCODE,txtLocationCode.Text,txtGoodsCode.Text, rdoRelation.Checked?1:0);

                    if (re.RESULT=="1")
                    {
                        //lstLocationGoods.Clear();
                        //this.bsLocationGoodsSource.DataSource = null;
                        //this.dtGriLocationdGoods.DataSource = this.bsLocationGoodsSource;

                       txtGoodsCode.Text = string.Empty;
                       txtLocationCode.Text = string.Empty;
                       txtLocationCode.Focus();
                       lblMessage.Text = "提交成功！";
                       lblMessage.ForeColor = Color.Blue;
                    }
                    else
                    {
                        lblMessage.Text = "提交失败！" + re.MESSAGE;
                        lblMessage.ForeColor = Color.Red;
                        //MessageBox.Show("提交失败！" + re.MESSAGE);
                        return;
                    }
                
            }

            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

 

        private void rdoScrap_Click(object sender, EventArgs e)
        {
            this.rdoRelation.Checked = false;
            this.rdoScrap.Checked = true;
        }

        private void rdoRelation_Click(object sender, EventArgs e)
        {
 

            this.rdoRelation.Checked = true;
            this.rdoScrap.Checked = false;
        }

        private void FrmLocationGoodsRelation_Load(object sender, EventArgs e)
        {
            rdoRelation.Checked = true;
        }

        private void txtGoodsCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            string goodsCode=txtGoodsCode.Text.ToString().Trim();
            if (string.IsNullOrEmpty(goodsCode))
            {
                MessageBox.Show("商品条码不能为空！");
                txtGoodsCode.SelectAll();
                return; 
            }
            if (goodsCode.Substring(0, 1) == "0" && goodsCode.Length == 18)
            {
                goodsCode = goodsCode.Substring(0, 10);
            }
            else if (goodsCode.Substring(0, 3) == "WOC" && (goodsCode.Length == 17 || goodsCode.Length == 20))
            {
                goodsCode = goodsCode.Substring(0, goodsCode.Length - 8);
            }
            else
            {
                MessageBox.Show("请扫描正确的商品条码！");
                txtGoodsCode.SelectAll();
                return; 
            }
            txtGoodsCode.Text = goodsCode;
         
        }

        private void txtLocationCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            string location = txtLocationCode.Text.Trim();
            if (string.IsNullOrEmpty(location))
            {
                MessageBox.Show("货位不能为空！");
                txtLocationCode.SelectAll();
                return;
            }
            this.txtGoodsCode.Focus();
        }
        //private void txtLocationCode_KeyDown(object sender, KeyEventArgs e)
        //{
        //    if (e.KeyCode != Keys.Enter) return;
        //    Cursor.Current = Cursors.WaitCursor;
        //     LocationGoodsInfo _LocationGoods =null;
        //    try
        //    {
        //        _LocationGoods = Duty.PickLocationRelationUIRule.NewInstance.ExistLoctionGoodsByLocationCode(txtLocationCode.Text).DATA;
                 
        //        if (_LocationGoods == null)
        //        {
        //            MessageBox.Show("找不到该货位信息");
        //            txtLocationCode.SelectAll();
        //            return;
        //        }
        //        if ( _LocationGoods.LOCATIONGOODSID > 0)
        //        {
        //            string Message = string.Format("货位:{0}, 商品名称:{1},已存在关联 \n 是否删除该关联？", txtLocationCode.Text, _LocationGoods.GOODSNAME);
        //            if (MessageBoxEx.Question(Message) == DialogResult.No)
        //            {
        //                txtLocationCode.SelectAll();
        //                txtLocationCode.Text = string.Empty;
        //                return;
        //            }
        //            if (!Duty.PickLocationRelationUIRule.NewInstance.DeleteLoctionGoodsRelation(_LocationGoods.LOCATIONGOODSID).DATA)
        //            {
        //                MessageBox.Show("删除失败，请重试！");
        //                txtLocationCode.SelectAll();
        //                return;
        //            }
        //            else
        //            {
        //                _LocationGoods = new LocationGoodsInfo() { LOCATIONCODE = _LocationGoods.LOCATIONCODE, LOCATIONID = _LocationGoods.LOCATIONID,GOODSNAME="",GOODSCODE="" };
        //            }
        //        }
                 
        //        int curIndex = lstLocationGoods.FindIndex(x => x.LOCATIONID == _LocationGoods.LOCATIONID);
        //        if (curIndex >= 0)
        //        {
        //            this.dtGriLocationdGoods.Select(curIndex);
        //        }
        //        else
        //        {
        //            lstLocationGoods.Add(_LocationGoods);
        //            this.bsLocationGoodsSource.DataSource = null;
        //            this.bsLocationGoodsSource.DataSource = lstLocationGoods;
        //            this.dtGriLocationdGoods.DataSource = this.bsLocationGoodsSource;
        //            int  index = lstLocationGoods.FindIndex(x => x.LOCATIONID == _LocationGoods.LOCATIONID);
        //            if (index >= 0)
        //            {
        //                this.dtGriLocationdGoods.Select(index);
        //            }
        //        }
              
        //      txtGoodsCode.SelectAll();
        //      txtGoodsCode.Focus();
        //    }

        //    catch (Exception ex)
        //    {
        //        WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
        //    }
        //    finally
        //    {
        //        Cursor.Current = Cursors.Default;
        //    }
        //}

        //private void txtGoodsCode_KeyDown(object sender, KeyEventArgs e)
        //{
        //    //if (e.KeyCode != Keys.Enter) return;
        //    //Cursor.Current = Cursors.WaitCursor;

        //    //try
        //    //{
        //    //    string locationCode = txtLocationCode.Text;
        //    //    if (string.IsNullOrEmpty(locationCode))
        //    //    {
        //    //        MessageBox.Show("请先扫描货位！");
        //    //        txtLocationCode.SelectAll();
        //    //        return;
        //    //    }
        //    //    if (!txtGoodsCode.Text.StartsWith("WOC"))
        //    //    {
        //    //        MessageBox.Show("请扫描WOC开头的商品！");
        //    //        txtGoodsCode.SelectAll();
        //    //        return;
        //    //    }

        //    //    LocationGoodsInfo _LocationGoods = Duty.PickLocationRelationUIRule.NewInstance.ExistLoctionGoodsByGoodsCode(txtGoodsCode.Text).DATA;
                
        //    //    if (_LocationGoods == null)
        //    //    {
        //    //        MessageBox.Show("找不到该商品信息");
        //    //        txtLocationCode.SelectAll();
        //    //        return;
        //    //    }
        //    //    if (_LocationGoods != null && _LocationGoods.LOCATIONGOODSID > 0)
        //    //    {
        //    //        string Message = string.Format("商品:{0}, 货位:{1},已存在关联 \n 是否删除该关联？", _LocationGoods.GOODSNAME, _LocationGoods.LOCATIONCODE);
        //    //        if (MessageBoxEx.Question(Message) == DialogResult.No)
        //    //        {
        //    //            txtGoodsCode.SelectAll();
        //    //            txtGoodsCode.Text = string.Empty;
        //    //            return;
        //    //        }
        //    //        if (!Duty.PickLocationRelationUIRule.NewInstance.DeleteLoctionGoodsRelation(_LocationGoods.LOCATIONGOODSID).DATA)
        //    //        {
        //    //            MessageBox.Show("删除失败，请重试！");
        //    //            txtGoodsCode.SelectAll();
        //    //            return;
        //    //        }
        //    //    }
        //    //    var curIndex = lstLocationGoods.FindIndex(x => x.LOCATIONCODE == locationCode);
        //    //    if (curIndex < 0)
        //    //    {
        //    //        MessageBox.Show("货位记录不存在，请先扫描货位！");
        //    //        return;
        //    //    }
        //    //    lstLocationGoods[curIndex].GOODSNAME = _LocationGoods.GOODSNAME;
        //    //    lstLocationGoods[curIndex].GOODSID = _LocationGoods.GOODSID;
        //    //    lstLocationGoods[curIndex].GOODSCODE = _LocationGoods.GOODSCODE;
        //    //    //lstLocationGoods[curIndex].GOODSID3 = _LocationGoods.GOODSID;
        //    //    //lstLocationGoods[curIndex].GOODSCODE3 = _LocationGoods.GOODSCODE;
        //    //    this.bsLocationGoodsSource.DataSource = null;
        //    //    this.bsLocationGoodsSource.DataSource = lstLocationGoods;
        //    //    this.dtGriLocationdGoods.DataSource = this.bsLocationGoodsSource;
        //    //    this.dtGriLocationdGoods.Refresh();
        //    //    this.dtGriLocationdGoods.Select(curIndex);
        //    //    txtLocationCode.Text = string.Empty;
        //    //    txtGoodsCode.Text = string.Empty;
        //    //    txtLocationCode.Focus();
        //    //}

        //    //catch (Exception ex)
        //    //{
        //    //    WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
        //    //}
        //    //finally
        //    //{
        //    //    Cursor.Current = Cursors.Default;
        //    //}

        //}

        //private void dtGriLocationdGoods_Click(object sender, EventArgs e)
        //{
        //    int curIndex = dtGriLocationdGoods.CurrentRowIndex;
        //    if (curIndex < 0) return;
        //    txtLocationCode.Text = dtGriLocationdGoods[curIndex, 0].ToString();
        //    txtGoodsCode.Text = dtGriLocationdGoods[curIndex, 2].ToString();
        //    this.dtGriLocationdGoods.Select(curIndex);
        //}

        //private void dtGriLocationdGoods_DoubleClick(object sender, EventArgs e)
        //{
        //    int curIndex = dtGriLocationdGoods.CurrentRowIndex;
        //    if (curIndex < 0) return;
        //    try
        //    {
        //        string locationCode = dtGriLocationdGoods[curIndex, 0].ToString();
        //        LocationGoodsInfo curItem =lstLocationGoods.Find(x => x.LOCATIONCODE ==locationCode);

        //        string Message = string.Format("名称:{0}, 货位:{1} \n 是否删除该数据？", curItem.GOODSNAME, curItem.LOCATIONCODE);
        //        if (MessageBoxEx.Question(Message) == DialogResult.Yes)
        //        {
        //            lstLocationGoods.Remove(curItem);
        //            this.bsLocationGoodsSource.DataSource = null;
        //            this.bsLocationGoodsSource.DataSource = lstLocationGoods;
        //            this.dtGriLocationdGoods.DataSource = this.bsLocationGoodsSource;
        //            this.dtGriLocationdGoods.Refresh();
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        TraceEx.Write(ex.Message);
        //        WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("删除数据失败!");
        //    }
        //    finally
        //    {
        //        txtGoodsCode.Focus();
        //        txtGoodsCode.SelectAll();
        //    }
        //}
    }
}